package com.codedancer.campus.client.api.circle;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.client.api.circle.ro.CircleCreateRo;
import com.codedancer.campus.client.api.circle.ro.CirclePageSearchRo;
import com.codedancer.campus.client.service.ICircleService;
import com.codedancer.campus.client.service.bo.circle.CircleCreateBo;
import com.codedancer.campus.client.service.bo.circle.CirclePageSearchBo;
import com.codedancer.campus.client.service.vo.circle.CirclePageVo;
import com.codedancer.campus.client.service.vo.circle.CircleVo;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "圈子api", description = "圈子api", tags = "圈子api")
@RequestMapping("/api/circle")
public class CircleApi extends BaseApi {

    @Autowired
    private ICircleService circleService;

    @PostMapping("/create")
    @ApiOperation("创建圈子")
    public ApiResponse<Void> create(@RequestBody @Valid @ApiParam(value = "圈子创建Ro", required = true)
        CircleCreateRo circleCreateRo) {
        CircleCreateBo circleCreateBo = BeanUtil
            .copyProperties(circleCreateRo, CircleCreateBo.class);
        circleCreateBo.setCurrentUserUuid(currentLoginUuid());
        circleService.create(circleCreateBo);
        return success();
    }

    @GetMapping("/top10")
    @ApiOperation("查询圈子排名前十")
    public ApiResponse<List<CircleVo>> top10() {
        return success(circleService.top10(currentLoginUuid()));
    }

    @PostMapping("/follow/{uuid}")
    @ApiOperation("关注圈子")
    public ApiResponse<Void> follow(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid) {
        circleService.follow(uuid, currentLoginUuid());
        return success();
    }

    @PostMapping("/cancel-follow/{uuid}")
    @ApiOperation("取消关注")
    public ApiResponse<Void> cancelFollow(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid) {
        circleService.cancelFollow(uuid, currentLoginUuid());
        return success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询圈子")
    public ApiResponse<IPage<CirclePageVo>> page(@ApiParam(value = "圈子分页查询Ro", required = false)
        CirclePageSearchRo circlePageSearchRo) {
        CirclePageSearchBo circlePageSearchBo = BeanUtil
            .copyProperties(circlePageSearchRo, CirclePageSearchBo.class);
        circlePageSearchBo.setCurrentUserUuid(currentLoginUuid());
        return success(circleService.page(circlePageSearchBo));
    }

    @GetMapping("/detail/{uuid}")
    @ApiOperation("圈子详情")
    public ApiResponse<CircleVo> detail(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid) {
        return success(circleService.detail(uuid, currentLoginUuid()));
    }

    @GetMapping("/list-by-school")
    @ApiOperation("获取某个学校全部圈子")
    public ApiResponse<List<CircleVo>> listBySchoolUuid() {
        return success(circleService.listBySchoolUuid(currentLoginUuid()));
    }

    @GetMapping("/page/user")
    @ApiOperation("分页查询用户关注或创建的圈子")
    public ApiResponse<IPage<CirclePageVo>> page2User(
        @ApiParam(value = "圈子分页查询Ro", required = false)
            CirclePageSearchRo circlePageSearchRo) {
        CirclePageSearchBo circlePageSearchBo = BeanUtil
            .copyProperties(circlePageSearchRo, CirclePageSearchBo.class);
        circlePageSearchBo.setCurrentUserUuid(currentLoginUuid());
        return success(circleService.page2User(circlePageSearchBo ));
    }

    @PutMapping("/enable/{uuid}")
    @ApiOperation("启用圈子")
    public ApiResponse<Void> enable(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid) {
        circleService.enable(uuid);
        return success();
    }

    @PutMapping("/disable/{uuid}")
    @ApiOperation("禁用圈子")
    public ApiResponse<Void> disable(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid) {
        circleService.disable(uuid);
        return success();
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除圈子")
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid) {
        circleService.delete(uuid);
        return success();
    }

    @PutMapping("/update/{uuid}")
    @ApiOperation("修改圈子")
    public ApiResponse<Void> update(@RequestBody @Valid @ApiParam(value = "圈子修改Ro", required = true)
        CircleCreateRo circleCreateRo,
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "圈子uuid", required = true) String uuid) {
        CircleCreateBo circleCreateBo = BeanUtil
            .copyProperties(circleCreateRo, CircleCreateBo.class);
        circleCreateBo.setCurrentUserUuid(currentLoginUuid());
        circleCreateBo.setUuid(uuid);
        circleService.update(circleCreateBo);
        return success();
    }
}
